<?php
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
use Think\Cache\Driver\Redis;
use Think\Controller;

class StateController extends Controller{

    public function index(){
        set_time_limit(0);
        $news = M("vrnews1.news", null);
        $redis = new Redis();
        $redis->set(C('environment').'_limit', 0);
        $news_list = $news->where("type in(2,6,7)")->order('news_id desc')->select();
        ob_start();
        $i = 0;
        $j = 1;
        foreach ($news_list as $key=>$data){
            $j++;
            $redis->set(C('environment').'_limit', $j);
            if($i == 500){
                sleep(2);
                $i = 0;
            }
            $file_name = SITE_PATH . '/data/html/text/' . $data['news_id'] . '.html';
//            if(file_exists($file_name)){
//                continue;
//            }
            require_once(SITE_PATH . '/template/image_text_batch.html');
            $content = ob_get_contents();
            if (!$content) {
                continue;
            }
            $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https' : 'http';
            $jssdk = new JssdkController("wx46fc101ae0317865", "ade9338f307169231203492f5e248ec6");
            $signPackage = $jssdk->GetSignPackage();
            $appId = $signPackage["appId"];
            $timestamp = $signPackage["timestamp"];
            $nonceStr = $signPackage["nonceStr"];
            $signature = $signPackage["signature"];
            $share = C("admin_host").'/text/' . $data['news_id'] . '.html';
            if($data['new_abstract']){
                $description = $data['new_abstract'];
            }else{
                if($data['abstract']){
                    $description =  $data['abstract'];
                }else{
                    $description =  'live.lawnewsw.com';
                }
            }
            if($data['new_cover_image']){
                $cover_img =  $data['new_cover_image'];
            }else{
                if($data['cover_image']){
                    $cover_img = $data['cover_image'];
                }else{
                    $cover_img = C('h5_host').'/public/H5/images/default.png';
                }
            }
            if($data['new_cover_image']){
                $news_cover_img =  $data['new_cover_image'];
            }else{
                if($data['cover_image']){
                    $news_cover_img =  $data['cover_image'];
                }else{
                    $news_cover_img =  C('h5_host').'/public/H5/images/default.png';
                }
            }
            if($data['new_abstract']){
                $new_abstract = $data['new_abstract'];
            }else{
                if($data['abstract']){
                    $new_abstract = $data['abstract'];
                }else{
                    $new_abstract = 'live.lawnewsw.com';
                }
            }
            $time = date('m月d日 H:i',strtotime($data['create_time']));
            if($data['source_name']) {
                $source = '来源：<span ><a style="color:#129aee; display:inline;" href="#" onclick="toSourceId(\'' . $data["source_id"] . '\',\'' . $data["source_name"] . '\')" >' . $data["source_name"] . '</a></span>';
            }else{
                $source = '';
            }
            if($data['type'] == 7){
                $news_content = $this->handleImage($data['content']);
            }else{
                $news_content = json_decode($data['content']);
            }
            if(!empty($data['outer_url'])) {
                $out_url = '<div class="ReadText" style="float:right;margin:0 5%;font-size:1.3rem"><a href="' . $data['outer_url'] . '" style="color: #2ea7e0;">阅读原文</a></div>';
            }else{
                $out_url = '';
            }
            if($data['new_title']){
                $title = $data['new_title'];
            }else{
                $title = $data['title'];
            }
            $h5_host = C("h5_host");
            $host    = C("host");
            $content = str_replace('{{appId}}',$appId ,$content );
            $content = str_replace('{{timestamp}}',$timestamp ,$content );
            $content = str_replace('{{nonceStr}}',$nonceStr ,$content );
            $content = str_replace('{{signature}}',$signature ,$content );
            $content = str_replace('{{share}}',$share ,$content );
            $content = str_replace('{{description}}',$description ,$content );
            $content = str_replace('{{title1}}',$title ,$content );
            $content = str_replace('{{cove_img}}',$cover_img ,$content );
            $content = str_replace('{{new_cover_image}}',$news_cover_img ,$content );
            $content = str_replace('{{title}}',$data['title'] ,$content );
            $content = str_replace('{{abstract}}',$new_abstract ,$content );
            $content = str_replace('{{data}}',$time ,$content );
            $content = str_replace('{{source}}',$source ,$content );
            $content = str_replace('{{content}}',$news_content ,$content );
            $content = str_replace('{{out_url}}',$out_url ,$content );
            $content = str_replace('{{h5_host}}',$h5_host ,$content );
            $content = str_replace('{{host}}',$host ,$content );
            $handle = fopen($file_name, 'w');
            file_put_contents($file_name, $content);
            fclose($handle);
            $i++;
        }
        $redis->set(C('environment').'_limit', $j);
    }

    public function photos(){
        set_time_limit(0);
        $news = M("vrnews1.news", null);
        $redis = new Redis();
        $redis->set(C('environment').'_limit_photo',0);
        $news_list = $news->where("type = 5")->order("news_id desc")->select();
        ob_start();
        $i = 0;
        $j = 0;
        foreach ($news_list as $key=>$data){
            $j++;
            $redis->set(C('environment').'_limit_photo',$j);
            if($i == 500){
                sleep(2);
                $i = 0;
            }
            $file_name = SITE_PATH.'/data/html/photos/'.$data['news_id'].'.html';
//            if(file_exists($file_name)){
//                continue;
//            }
            require_once (SITE_PATH.'/template/images_batch.html');
            $content = ob_get_contents();
            if(!$content){
                continue;
            }
            $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https' : 'http';
            $jssdk = new JssdkController("wx46fc101ae0317865", "ade9338f307169231203492f5e248ec6");
            $signPackage = $jssdk->GetSignPackage();
            $appId = $signPackage["appId"];
            $timestamp = $signPackage["timestamp"];
            $nonceStr = $signPackage["nonceStr"];
            $signature = $signPackage["signature"];
            $share = $share = C("admin_host").'/photos/' . $data['news_id'] . '.html';
            $description = $data['abstract'] ? $data['abstract'] : 'live.lawnewsw.com';
            $contents = json_decode($data['content'],true);
            if($data['new_title']){
                $title = $data['new_title'];
            }else{
                $title = $data['title'];
            }
            if($data['new_cover_image']){
                $cover_img =  $data['new_cover_image'];
            }else{
                if($data['cover_image']){
                    $cover_img = $data['cover_image'];
                }else{
                    $cover_img = C('h5_host').'/public/H5/images/default.png';
                }
            }
            if($data['new_cover_image']){
                $news_cover_img =  $data['new_cover_image'];
            }else{
                if($data['cover_image']){
                    $news_cover_img =  $data['cover_image'];
                }else{
                    $news_cover_img =  C('h5_host').'/public/H5/images/default.png';
                }
            }
            if($data['new_abstract']){
                $new_abstract = $data['new_abstract'];
            }else{
                if($data['abstract']){
                    $new_abstract = $data['abstract'];
                }else{
                    $new_abstract = 'live.lawnewsw.com';
                }
            }
            $text = '';
            foreach($contents as $key=>$val){
                $text .= '<img src="'.$val['img'].'/y" name="'.$key.'" onclick="clickImage()">';
            }
            $news_content = '';
            foreach($contents as $keys=>$value){
                $news_content .= '<div class="neirong" ';
                if($keys == 0) {$news_content .= 'style="display:block;" ';}
                $news_content .= '><div class="biaoti">
                        <div class="left">'.$data['title'].'</div>
                        <div class="right"></div>
                    </div>
                    <p>'.$value['text'].'</p>
                </div>';
            }
            $h5_host = C("h5_host");
            $host    = C("host");
            $content = str_replace('{{appId}}',$appId ,$content );
            $content = str_replace('{{timestamp}}',$timestamp ,$content );
            $content = str_replace('{{nonceStr}}',$nonceStr ,$content );
            $content = str_replace('{{signature}}',$signature ,$content );
            $content = str_replace('{{title}}',$title ,$content );
            $content = str_replace('{{share}}',$share ,$content );
            $content = str_replace('{{description}}',$description ,$content );
            $content = str_replace('{{cove_img}}',$cover_img ,$content );
            $content = str_replace('{{new_cover_img}}',$news_cover_img ,$content );
            $content = str_replace('{{text}}',$text ,$content );
            $content = str_replace('{{abstract}}',$new_abstract ,$content );
            $content = str_replace('{{content}}',$news_content ,$content );
            $content = str_replace('{{h5_host}}',$h5_host ,$content );
            $content = str_replace('{{host}}',$host ,$content );
            $handle = fopen($file_name, 'w');
            file_put_contents($file_name, $content);
            fclose($handle);
            $i++;
        }
        $redis->set(C('environment').'_limit_photo',$j);
    }

    public function handleImage($content){
        $content = json_decode($content);
        $content = preg_replace('/(src=\"http:\/\/vrlive-.*?(jpg|png|jpeg))\"/', '$1/y"', $content);
        return $content;
    }

    public function addPower(){
        $menu = M('vradmin1.admin_user',null)->field('admin_id')->select();
        $sys_admin = M('vradmin1.sys_menu_admin',null);
        if($menu){
            foreach ($menu as $k=>$value){
                $is_have = $sys_admin->where("menu_id = 317 and admin_id = '" . $value['admin_id'] . "'")->count();
                if($is_have == 0){
                    $data['menu_id'] = 317;
                    $data['admin_id'] = $value['admin_id'];
                    $data_list[] = $data;
                }
            }
//            var_dump($data_list);exit();
            $sys_admin->addAll($data_list);
        }
    }
}